package cate.game.activity.luckycharm;

import cate.common.table.activity.luckycharm.LuckyCharmRow;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.game.activity.luckycharm.msg.LuckyCharmDataResp;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import com.fasterxml.jackson.annotation.JsonIgnore;
import cp.solution.util.time.DateKit;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;

public class RoleLuckyCharm extends RoleDependent {
    @NoteField("激活时间")
    public long activeTime;
    @NoteField("活动结束时间")
    public long endTime;
    @NoteField("渠道 1 小游戏 2 h5/微端")
    public byte channel;
    @NoteField("领奖记录 <阶段（1，2，3），是否领取>")
    public Map<Integer, Boolean> gotMap;
    @NoteField("阶段2开启时间,为0就是未解锁")
    public long period2ActiveTime;
    @NoteField("阶段3开启时间,为0就是未解锁")
    public long period3ActiveTime;
    @NoteField("永久结束")
    public boolean over;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (gotMap == null) {
            gotMap = new HashMap<>();
        }
    }

    @JsonIgnore
    public GameResult<Void> getReward(int period) {
        GameResult<Void> r = new GameResult<>();
        if (over) {
            return r.fail("活动已经结束拉");
        }
        LuckyCharmRow row = role.getGame().table.luckyCharm.base.get(period);
        if (row == null) {
            return r.fail("奖励不存在");
        }
        if (activeTime == 0) {
            return r.fail("您还未激活该活动噢");
        }
        if (System.currentTimeMillis() > endTime) {
            return r.fail("活动已经结束了噢");
        }
        if (gotMap.containsKey(period)) {
            return r.fail("您已经领取过该奖励");
        }
        if (period == 2) {
            if (period2ActiveTime == 0) {
                return r.fail("您还未解锁第二阶段");
            }
            if (System.currentTimeMillis() < period2ActiveTime) {
                return r.fail("倒计时未结束");
            }
            if (period3ActiveTime == 0) {
                LuckyCharmRow nextPeriodRow = role.getGame().table.luckyCharm.base.get(period + 1);
                if (nextPeriodRow != null) {
                    period3ActiveTime = System.currentTimeMillis() + nextPeriodRow.expireTime * 1000L;
                }
            }
        } else if (period == 3) {
            if (period3ActiveTime == 0) {
                return r.fail("您还未解锁第三阶段");
            }
            if (System.currentTimeMillis() < period3ActiveTime) {
                return r.fail("倒计时未结束");
            }
        }
        gotMap.put(period, true);
        new MixResAdder()
                .setOperation(GDOperation.LUCKY_CHARM_PERIOD_REWARD)
                .setOperationSub(period)
                .setRes(row.rewardStr)
                .setDialogReward(true)
                .setSendMailWhenFull(true)
                .exe(role);
        noticeUpdate();
        return r;
    }

    private static final long ACT_TIME = 4 * DateKit.DAY_MS;
    private static final long ACT_RECHECT_TIME = 7 * DateKit.DAY_MS;
    public void active(byte channel, boolean active) {
        if (over) {
            return;
        }
        if (!active) {
            return;
        }
        if (channel != Channel.WEIXIN.getType() && channel != Channel.H5_MINI.getType()) {
            return;
        }
        if (this.channel == 0) {
            this.channel = channel;
        }
        if (channel == Channel.H5_MINI.getType() && this.channel != Channel.H5_MINI.getType()) {
            this.channel = channel;
        }
        long now = System.currentTimeMillis();
        if (endTime > 0) {
            if (now > endTime && this.channel == Channel.H5_MINI.getType()) {
                over = true;
            }
            if (now > endTime + ACT_RECHECT_TIME && this.channel == Channel.WEIXIN.getType()) {
                this.activeTime = now;
                this.endTime = now + ACT_TIME;
                this.period2ActiveTime = 0;
                this.period3ActiveTime = 0;
            }
        } else if (activeTime == 0){
            this.activeTime = now;
            this.endTime = now + ACT_TIME;
        }
        if (this.channel == Channel.H5_MINI.getType() && this.period2ActiveTime == 0) {
            this.period2ActiveTime = now;
        }
        noticeUpdate();
    }

    @Override
    public void onDaySpan(boolean silence) {

    }

    public void noticeUpdate() {
        role.sendNow(new LuckyCharmDataResp(this));
    }

    enum Channel {
        /*微信*/
        WEIXIN(1),
        /*h5微端*/
        H5_MINI(2);
        private int type;
        Channel(int type) {
            this.type = type;
        }

        public int getType() {
            return this.type;
        }
    }
}
